1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-24 03:33:20 +01:00
llvm-mirror/lib/Support/BinaryStreamWriter.cpp
Zachary Turner fc2ee0d542 Make codeview::StringTable.
Previously we had knowledge of how to serialize and deserialize
a string table inside of DebugInfo/PDB, but the string table
that it serializes contains a piece that is actually considered
CodeView and can appear outside of a PDB.  We already have logic
in llvm-readobj and MCCodeView to read and write this format,
so it doesn't make sense to duplicate the logic in DebugInfoPDB
as well.

This patch makes codeview::StringTable (for writing) and
codeview::StringTableRef (for reading), updates DebugInfoPDB
to use these classes for its own writing, and updates llvm-readobj
to additionally use StringTableRef for reading.

It's a bit more difficult to get MCCodeView to use this for
writing, but it's a logical next step.

llvm-svn: 301986
2017-05-02 23:36:17 +00:00

82 lines
2.6 KiB
C++

//===- BinaryStreamWriter.cpp - Writes objects to a BinaryStream ----------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/BinaryStreamWriter.h"
#include "llvm/Support/BinaryStreamError.h"
#include "llvm/Support/BinaryStreamReader.h"
#include "llvm/Support/BinaryStreamRef.h"
using namespace llvm;
BinaryStreamWriter::BinaryStreamWriter(WritableBinaryStreamRef S)
: Stream(S), Offset(0) {}
Error BinaryStreamWriter::writeBytes(ArrayRef<uint8_t> Buffer) {
if (auto EC = Stream.writeBytes(Offset, Buffer))
return EC;
Offset += Buffer.size();
return Error::success();
}
Error BinaryStreamWriter::writeCString(StringRef Str) {
if (auto EC = writeFixedString(Str))
return EC;
if (auto EC = writeObject('\0'))
return EC;
return Error::success();
}
Error BinaryStreamWriter::writeFixedString(StringRef Str) {
return writeBytes(ArrayRef<uint8_t>(Str.bytes_begin(), Str.bytes_end()));
}
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref) {
return writeStreamRef(Ref, Ref.getLength());
}
Error BinaryStreamWriter::writeStreamRef(BinaryStreamRef Ref, uint32_t Length) {
BinaryStreamReader SrcReader(Ref.slice(0, Length));
// This is a bit tricky. If we just call readBytes, we are requiring that it
// return us the entire stream as a contiguous buffer. There is no guarantee
// this can be satisfied by returning a reference straight from the buffer, as
// an implementation may not store all data in a single contiguous buffer. So
// we iterate over each contiguous chunk, writing each one in succession.
while (SrcReader.bytesRemaining() > 0) {
ArrayRef<uint8_t> Chunk;
if (auto EC = SrcReader.readLongestContiguousChunk(Chunk))
return EC;
if (auto EC = writeBytes(Chunk))
return EC;
}
return Error::success();
}
std::pair<BinaryStreamWriter, BinaryStreamWriter>
BinaryStreamWriter::split(uint32_t Off) const {
assert(getLength() >= Off);
WritableBinaryStreamRef First = Stream.drop_front(Offset);
WritableBinaryStreamRef Second = First.drop_front(Off);
First = First.keep_front(Off);
BinaryStreamWriter W1{First};
BinaryStreamWriter W2{Second};
return std::make_pair(W1, W2);
}
Error BinaryStreamWriter::padToAlignment(uint32_t Align) {
uint32_t NewOffset = alignTo(Offset, Align);
if (NewOffset > getLength())
return make_error<BinaryStreamError>(stream_error_code::stream_too_short);
Offset = NewOffset;
return Error::success();
}