brintos

brintos / llvm-project-archived public Read only

0
0
Text · 1.7 KiB · 6c26ec4 Raw
59 lines · cpp
1//===- DXContainerWriter.cpp ----------------------------------------------===//2//3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.4// See https://llvm.org/LICENSE.txt for license information.5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception6//7//===----------------------------------------------------------------------===//8 9#include "DXContainerWriter.h"10 11namespace llvm {12namespace objcopy {13namespace dxbc {14 15using namespace object;16 17size_t DXContainerWriter::finalize() {18  assert(Offsets.empty() &&19         "Attempted to finalize writer with already computed offsets");20  Offsets.reserve(Obj.Parts.size());21  size_t Offset = Obj.headerSize();22  for (const Part &P : Obj.Parts) {23    Offsets.push_back(Offset);24    Offset += P.size();25  }26  return Obj.Header.FileSize;27}28 29Error DXContainerWriter::write() {30  size_t TotalSize = finalize();31  Out.reserveExtraSpace(TotalSize);32 33  llvm::dxbc::Header Header = Obj.Header;34  if (sys::IsBigEndianHost)35    Header.swapBytes();36  Out.write(reinterpret_cast<const char *>(&Header),37            sizeof(::llvm::dxbc::Header));38  if (sys::IsBigEndianHost)39    for (auto &O : Offsets)40      sys::swapByteOrder(O);41  Out.write(reinterpret_cast<const char *>(Offsets.data()),42            Offsets.size() * sizeof(uint32_t));43 44  for (const Part &P : Obj.Parts) {45    Out.write(reinterpret_cast<const char *>(P.Name.data()), 4);46    uint32_t Size = P.Data.size();47    if (sys::IsBigEndianHost)48      sys::swapByteOrder(Size);49    Out.write(reinterpret_cast<const char *>(&Size), sizeof(uint32_t));50    Out.write(reinterpret_cast<const char *>(P.Data.data()), P.Data.size());51  }52 53  return Error::success();54}55 56} // end namespace dxbc57} // end namespace objcopy58} // end namespace llvm59