GeneratorUtil.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * Copyright (c) 2021-2024, Tim Flynn <trflynn89@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/ByteString.h>
  8. #include <AK/HashMap.h>
  9. #include <AK/NumericLimits.h>
  10. #include <AK/SourceGenerator.h>
  11. #include <AK/StringView.h>
  12. #include <AK/Vector.h>
  13. #include <LibCore/File.h>
  14. template<typename StorageType>
  15. class UniqueStorage {
  16. public:
  17. size_t ensure(StorageType value)
  18. {
  19. // We maintain a set of unique values in two structures: a vector which stores the values in
  20. // the order they are added, and a hash map which maps that value to its index in the vector.
  21. // The vector is to ensure the values are generated in an easily known order, and the map is
  22. // to allow quickly deciding if a value is actually unique (otherwise, we'd have to linearly
  23. // search the vector for each value).
  24. //
  25. // Also note that index 0 will be reserved for the default-initialized value, so the index
  26. // returned from this method is actually the real index in the vector + 1.
  27. if (auto index = m_storage_indices.get(value); index.has_value())
  28. return *index;
  29. m_storage.append(move(value));
  30. auto storage_index = m_storage.size();
  31. m_storage_indices.set(m_storage.last(), storage_index);
  32. return storage_index;
  33. }
  34. StringView type_that_fits() const
  35. {
  36. if (m_storage.size() <= NumericLimits<u8>::max())
  37. return "u8"sv;
  38. if (m_storage.size() <= NumericLimits<u16>::max())
  39. return "u16"sv;
  40. if (m_storage.size() <= NumericLimits<u32>::max())
  41. return "u32"sv;
  42. return "u64"sv;
  43. }
  44. protected:
  45. Vector<StorageType> m_storage;
  46. HashMap<StorageType, size_t> m_storage_indices;
  47. };
  48. class UniqueStringStorage : public UniqueStorage<ByteString> {
  49. using Base = UniqueStorage<ByteString>;
  50. public:
  51. // The goal of the string table generator is to ensure the table is located within the read-only
  52. // section of the shared library. If StringViews are generated directly, the table will be located
  53. // in the initialized data section. So instead, we generate run-length encoded (RLE) arrays to
  54. // represent the strings.
  55. void generate(SourceGenerator& generator) const
  56. {
  57. constexpr size_t max_values_per_row = 300;
  58. size_t values_in_current_row = 0;
  59. auto append_hex_value = [&](auto value) {
  60. if (values_in_current_row++ > 0)
  61. generator.append(", ");
  62. generator.append(ByteString::formatted("{:#x}", value));
  63. if (values_in_current_row == max_values_per_row) {
  64. values_in_current_row = 0;
  65. generator.append(",\n ");
  66. }
  67. };
  68. Vector<u32> string_indices;
  69. string_indices.ensure_capacity(Base::m_storage.size());
  70. u32 next_index { 0 };
  71. for (auto const& string : Base::m_storage) {
  72. // Ensure the string length may be encoded as two u8s.
  73. VERIFY(string.length() <= NumericLimits<u16>::max());
  74. string_indices.unchecked_append(next_index);
  75. next_index += string.length() + 2;
  76. }
  77. generator.set("size", ByteString::number(next_index));
  78. generator.append(R"~~~(
  79. static constexpr Array<u8, @size@> s_encoded_strings { {
  80. )~~~");
  81. for (auto const& string : Base::m_storage) {
  82. auto length = string.length();
  83. append_hex_value((length & 0xff00) >> 8);
  84. append_hex_value(length & 0x00ff);
  85. for (auto ch : string)
  86. append_hex_value(static_cast<u8>(ch));
  87. }
  88. generator.append(R"~~~(
  89. } };
  90. )~~~");
  91. generator.set("size", ByteString::number(string_indices.size()));
  92. generator.append(R"~~~(
  93. static constexpr Array<u32, @size@> s_encoded_string_indices { {
  94. )~~~");
  95. values_in_current_row = 0;
  96. for (auto index : string_indices)
  97. append_hex_value(index);
  98. generator.append(R"~~~(
  99. } };
  100. static constexpr StringView decode_string(size_t index)
  101. {
  102. if (index == 0)
  103. return {};
  104. index = s_encoded_string_indices[index - 1];
  105. auto length_high = s_encoded_strings[index];
  106. auto length_low = s_encoded_strings[index + 1];
  107. size_t length = (length_high << 8) | length_low;
  108. if (length == 0)
  109. return {};
  110. auto const* start = &s_encoded_strings[index + 2];
  111. return { reinterpret_cast<char const*>(start), length };
  112. }
  113. )~~~");
  114. }
  115. };
  116. inline ErrorOr<NonnullOwnPtr<Core::InputBufferedFile>> open_file(StringView path, Core::File::OpenMode mode)
  117. {
  118. if (path.is_empty())
  119. return Error::from_string_literal("Provided path is empty, please provide all command line options");
  120. auto file = TRY(Core::File::open(path, mode));
  121. return Core::InputBufferedFile::create(move(file));
  122. }