| 253 | } |
| 254 | |
| 255 | LogicalResult writeStringSection(raw_ostream &stream) { |
| 256 | SmallVector<char> buffer; |
| 257 | llvm::raw_svector_ostream sectionStream(buffer); |
| 258 | EncodingWriter sectionWriter(sectionStream); |
| 259 | sectionWriter.writeVarInt(stringIndexMap.size()); |
| 260 | // Align the string section |
| 261 | uint64_t alignmentNeeded = alignof(uint32_t); |
| 262 | sectionWriter.alignTo(alignmentNeeded); |
| 263 | |
| 264 | // Save the current position to fix up offsets later. |
| 265 | auto offsetsPtr = sectionWriter.tell(); |
| 266 | |
| 267 | // Reserve space for the offset table (filled later). |
| 268 | for (size_t i = 0; i < stringIndexMap.size(); ++i) |
| 269 | sectionWriter.writeLE<uint32_t>(0); |
| 270 | |
| 271 | // Write each string and record its starting offset. |
| 272 | SmallVector<uint32_t> finalOffsets; |
| 273 | finalOffsets.reserve(stringIndexMap.size()); |
| 274 | |
| 275 | uint32_t running = 0; |
| 276 | for (const auto &str : stringIndexMap) { |
| 277 | finalOffsets.push_back(running); |
| 278 | sectionWriter.write(str.first); |
| 279 | running += str.first.size(); |
| 280 | } |
| 281 | |
| 282 | // Copy the pre-computed offsets into the reserved slot. |
| 283 | std::copy_n(finalOffsets.begin(), finalOffsets.size(), |
| 284 | reinterpret_cast<uint32_t *>(buffer.data() + offsetsPtr)); |
| 285 | |
| 286 | writeSectionHeader(stream, Bytecode::Section::String, buffer.size(), |
| 287 | sectionWriter.getRequiredAlignment()); |
| 288 | stream.write(buffer.data(), buffer.size()); |
| 289 | return success(); |
| 290 | } |
| 291 | |
| 292 | private: |
| 293 | llvm::MapVector<StringRef, uint64_t> stringIndexMap; |
no test coverage detected