summaryrefslogtreecommitdiffstats
path: root/llvm/lib/DebugInfo/MSF/BinaryStreamWriter.cpp
blob: 08e4aef60603288baf04fdff4be55d1d91880791 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//===- 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/DebugInfo/MSF/BinaryStreamWriter.h"

#include "llvm/DebugInfo/MSF/BinaryStreamReader.h"
#include "llvm/DebugInfo/MSF/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::writeInteger(uint64_t Value, uint32_t ByteSize) {
  assert(ByteSize == 1 || ByteSize == 2 || ByteSize == 4 || ByteSize == 8);
  uint8_t Bytes[8];
  MutableArrayRef<uint8_t> Buffer(Bytes);
  Buffer = Buffer.take_front(ByteSize);
  switch (ByteSize) {
  case 1:
    Buffer[0] = static_cast<uint8_t>(Value);
    break;
  case 2:
    llvm::support::endian::write16(Buffer.data(), Value, Stream.getEndian());
    break;
  case 4:
    llvm::support::endian::write32(Buffer.data(), Value, Stream.getEndian());
    break;
  case 8:
    llvm::support::endian::write64(Buffer.data(), Value, Stream.getEndian());
    break;
  }
  return writeBytes(Buffer);
}

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();
}
OpenPOWER on IntegriCloud