| 345 | } |
| 346 | |
| 347 | LogicalResult writeTypeSection(raw_ostream &stream) { |
| 348 | SmallVector<char> buffer; |
| 349 | llvm::raw_svector_ostream sectionStream(buffer); |
| 350 | EncodingWriter sectionWriter(sectionStream); |
| 351 | |
| 352 | sectionWriter.writeVarInt(typeList.size()); |
| 353 | // Align the type section |
| 354 | uint64_t alignmentNeeded = alignof(uint32_t); |
| 355 | sectionWriter.alignTo(alignmentNeeded); |
| 356 | |
| 357 | // Save the current position to fix up offsets later. |
| 358 | auto offsetsPtr = sectionWriter.tell(); |
| 359 | |
| 360 | // Reserve space for the offset table (filled later). |
| 361 | for (size_t i = 0; i < typeList.size(); ++i) |
| 362 | sectionWriter.writeLE<uint32_t>(0); |
| 363 | |
| 364 | // Write each type and record its starting offset. |
| 365 | SmallVector<uint32_t> finalOffsets; |
| 366 | finalOffsets.reserve(typeList.size()); |
| 367 | |
| 368 | uint32_t running = 0; |
| 369 | for (Type type : typeList) { |
| 370 | finalOffsets.push_back(running); |
| 371 | auto before = sectionWriter.tell(); |
| 372 | if (failed(serializeType(type, sectionWriter))) |
| 373 | return failure(); |
| 374 | running += static_cast<uint32_t>(sectionWriter.tell() - before); |
| 375 | } |
| 376 | |
| 377 | // Copy the pre-computed offsets into the reserved slot. |
| 378 | std::copy_n(finalOffsets.begin(), finalOffsets.size(), |
| 379 | reinterpret_cast<uint32_t *>(buffer.data() + offsetsPtr)); |
| 380 | |
| 381 | writeSectionHeader(stream, Bytecode::Section::Type, buffer.size(), |
| 382 | sectionWriter.getRequiredAlignment()); |
| 383 | stream.write(buffer.data(), buffer.size()); |
| 384 | return success(); |
| 385 | } |
| 386 | |
| 387 | /// Helper function to write the index of a given type to the writer. |
| 388 | LogicalResult writeTypeIndex(Type type, EncodingWriter &writer) { |
no test coverage detected