10b57cec5SDimitry Andric //===- CodeViewRecordIO.cpp -------------------------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric
90b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeViewRecordIO.h"
100b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/CodeView.h"
110b57cec5SDimitry Andric #include "llvm/DebugInfo/CodeView/RecordSerialization.h"
120b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamReader.h"
130b57cec5SDimitry Andric #include "llvm/Support/BinaryStreamWriter.h"
140b57cec5SDimitry Andric
150b57cec5SDimitry Andric using namespace llvm;
160b57cec5SDimitry Andric using namespace llvm::codeview;
170b57cec5SDimitry Andric
beginRecord(Optional<uint32_t> MaxLength)180b57cec5SDimitry Andric Error CodeViewRecordIO::beginRecord(Optional<uint32_t> MaxLength) {
190b57cec5SDimitry Andric RecordLimit Limit;
200b57cec5SDimitry Andric Limit.MaxLength = MaxLength;
210b57cec5SDimitry Andric Limit.BeginOffset = getCurrentOffset();
220b57cec5SDimitry Andric Limits.push_back(Limit);
230b57cec5SDimitry Andric return Error::success();
240b57cec5SDimitry Andric }
250b57cec5SDimitry Andric
endRecord()260b57cec5SDimitry Andric Error CodeViewRecordIO::endRecord() {
270b57cec5SDimitry Andric assert(!Limits.empty() && "Not in a record!");
280b57cec5SDimitry Andric Limits.pop_back();
290b57cec5SDimitry Andric // We would like to assert that we actually read / wrote all the bytes that we
300b57cec5SDimitry Andric // expected to for this record, but unfortunately we can't do this. Some
310b57cec5SDimitry Andric // producers such as MASM over-allocate for certain types of records and
320b57cec5SDimitry Andric // commit the extraneous data, so when reading we can't be sure every byte
330b57cec5SDimitry Andric // will have been read. And when writing we over-allocate temporarily since
340b57cec5SDimitry Andric // we don't know how big the record is until we're finished writing it, so
350b57cec5SDimitry Andric // even though we don't commit the extraneous data, we still can't guarantee
360b57cec5SDimitry Andric // we're at the end of the allocated data.
370b57cec5SDimitry Andric
380b57cec5SDimitry Andric if (isStreaming()) {
390b57cec5SDimitry Andric // For streaming mode, add padding to align with 4 byte boundaries for each
400b57cec5SDimitry Andric // record
410b57cec5SDimitry Andric uint32_t Align = getStreamedLen() % 4;
420b57cec5SDimitry Andric if (Align == 0)
430b57cec5SDimitry Andric return Error::success();
440b57cec5SDimitry Andric
450b57cec5SDimitry Andric int PaddingBytes = 4 - Align;
460b57cec5SDimitry Andric while (PaddingBytes > 0) {
470b57cec5SDimitry Andric char Pad = static_cast<uint8_t>(LF_PAD0 + PaddingBytes);
480b57cec5SDimitry Andric StringRef BytesSR = StringRef(&Pad, sizeof(Pad));
495ffd83dbSDimitry Andric Streamer->emitBytes(BytesSR);
500b57cec5SDimitry Andric --PaddingBytes;
510b57cec5SDimitry Andric }
528bcb0991SDimitry Andric resetStreamedLen();
530b57cec5SDimitry Andric }
540b57cec5SDimitry Andric return Error::success();
550b57cec5SDimitry Andric }
560b57cec5SDimitry Andric
maxFieldLength() const570b57cec5SDimitry Andric uint32_t CodeViewRecordIO::maxFieldLength() const {
580b57cec5SDimitry Andric if (isStreaming())
590b57cec5SDimitry Andric return 0;
600b57cec5SDimitry Andric
610b57cec5SDimitry Andric assert(!Limits.empty() && "Not in a record!");
620b57cec5SDimitry Andric
630b57cec5SDimitry Andric // The max length of the next field is the minimum of all lengths that would
640b57cec5SDimitry Andric // be allowed by any of the sub-records we're in. In practice, we can only
650b57cec5SDimitry Andric // ever be at most 1 sub-record deep (in a FieldList), but this works for
660b57cec5SDimitry Andric // the general case.
670b57cec5SDimitry Andric uint32_t Offset = getCurrentOffset();
680b57cec5SDimitry Andric Optional<uint32_t> Min = Limits.front().bytesRemaining(Offset);
690b57cec5SDimitry Andric for (auto X : makeArrayRef(Limits).drop_front()) {
700b57cec5SDimitry Andric Optional<uint32_t> ThisMin = X.bytesRemaining(Offset);
710b57cec5SDimitry Andric if (ThisMin.hasValue())
720b57cec5SDimitry Andric Min = (Min.hasValue()) ? std::min(*Min, *ThisMin) : *ThisMin;
730b57cec5SDimitry Andric }
740b57cec5SDimitry Andric assert(Min.hasValue() && "Every field must have a maximum length!");
750b57cec5SDimitry Andric
760b57cec5SDimitry Andric return *Min;
770b57cec5SDimitry Andric }
780b57cec5SDimitry Andric
padToAlignment(uint32_t Align)790b57cec5SDimitry Andric Error CodeViewRecordIO::padToAlignment(uint32_t Align) {
800b57cec5SDimitry Andric if (isReading())
810b57cec5SDimitry Andric return Reader->padToAlignment(Align);
820b57cec5SDimitry Andric return Writer->padToAlignment(Align);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric
skipPadding()850b57cec5SDimitry Andric Error CodeViewRecordIO::skipPadding() {
860b57cec5SDimitry Andric assert(!isWriting() && "Cannot skip padding while writing!");
870b57cec5SDimitry Andric
880b57cec5SDimitry Andric if (Reader->bytesRemaining() == 0)
890b57cec5SDimitry Andric return Error::success();
900b57cec5SDimitry Andric
910b57cec5SDimitry Andric uint8_t Leaf = Reader->peek();
920b57cec5SDimitry Andric if (Leaf < LF_PAD0)
930b57cec5SDimitry Andric return Error::success();
940b57cec5SDimitry Andric // Leaf is greater than 0xf0. We should advance by the number of bytes in
950b57cec5SDimitry Andric // the low 4 bits.
960b57cec5SDimitry Andric unsigned BytesToAdvance = Leaf & 0x0F;
970b57cec5SDimitry Andric return Reader->skip(BytesToAdvance);
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric
mapByteVectorTail(ArrayRef<uint8_t> & Bytes,const Twine & Comment)1000b57cec5SDimitry Andric Error CodeViewRecordIO::mapByteVectorTail(ArrayRef<uint8_t> &Bytes,
1010b57cec5SDimitry Andric const Twine &Comment) {
1020b57cec5SDimitry Andric if (isStreaming()) {
1030b57cec5SDimitry Andric emitComment(Comment);
1045ffd83dbSDimitry Andric Streamer->emitBinaryData(toStringRef(Bytes));
1050b57cec5SDimitry Andric incrStreamedLen(Bytes.size());
1060b57cec5SDimitry Andric } else if (isWriting()) {
1070b57cec5SDimitry Andric if (auto EC = Writer->writeBytes(Bytes))
1080b57cec5SDimitry Andric return EC;
1090b57cec5SDimitry Andric } else {
1100b57cec5SDimitry Andric if (auto EC = Reader->readBytes(Bytes, Reader->bytesRemaining()))
1110b57cec5SDimitry Andric return EC;
1120b57cec5SDimitry Andric }
1130b57cec5SDimitry Andric return Error::success();
1140b57cec5SDimitry Andric }
1150b57cec5SDimitry Andric
mapByteVectorTail(std::vector<uint8_t> & Bytes,const Twine & Comment)1160b57cec5SDimitry Andric Error CodeViewRecordIO::mapByteVectorTail(std::vector<uint8_t> &Bytes,
1170b57cec5SDimitry Andric const Twine &Comment) {
1180b57cec5SDimitry Andric ArrayRef<uint8_t> BytesRef(Bytes);
1190b57cec5SDimitry Andric if (auto EC = mapByteVectorTail(BytesRef, Comment))
1200b57cec5SDimitry Andric return EC;
1210b57cec5SDimitry Andric if (!isWriting())
1220b57cec5SDimitry Andric Bytes.assign(BytesRef.begin(), BytesRef.end());
1230b57cec5SDimitry Andric
1240b57cec5SDimitry Andric return Error::success();
1250b57cec5SDimitry Andric }
1260b57cec5SDimitry Andric
mapInteger(TypeIndex & TypeInd,const Twine & Comment)1270b57cec5SDimitry Andric Error CodeViewRecordIO::mapInteger(TypeIndex &TypeInd, const Twine &Comment) {
1280b57cec5SDimitry Andric if (isStreaming()) {
1298bcb0991SDimitry Andric std::string TypeNameStr = Streamer->getTypeName(TypeInd);
1308bcb0991SDimitry Andric if (!TypeNameStr.empty())
1318bcb0991SDimitry Andric emitComment(Comment + ": " + TypeNameStr);
1328bcb0991SDimitry Andric else
1330b57cec5SDimitry Andric emitComment(Comment);
1345ffd83dbSDimitry Andric Streamer->emitIntValue(TypeInd.getIndex(), sizeof(TypeInd.getIndex()));
1350b57cec5SDimitry Andric incrStreamedLen(sizeof(TypeInd.getIndex()));
1360b57cec5SDimitry Andric } else if (isWriting()) {
1370b57cec5SDimitry Andric if (auto EC = Writer->writeInteger(TypeInd.getIndex()))
1380b57cec5SDimitry Andric return EC;
1390b57cec5SDimitry Andric } else {
1400b57cec5SDimitry Andric uint32_t I;
1410b57cec5SDimitry Andric if (auto EC = Reader->readInteger(I))
1420b57cec5SDimitry Andric return EC;
1430b57cec5SDimitry Andric TypeInd.setIndex(I);
1440b57cec5SDimitry Andric }
1450b57cec5SDimitry Andric return Error::success();
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric
mapEncodedInteger(int64_t & Value,const Twine & Comment)1480b57cec5SDimitry Andric Error CodeViewRecordIO::mapEncodedInteger(int64_t &Value,
1490b57cec5SDimitry Andric const Twine &Comment) {
1500b57cec5SDimitry Andric if (isStreaming()) {
1510b57cec5SDimitry Andric if (Value >= 0)
1520b57cec5SDimitry Andric emitEncodedUnsignedInteger(static_cast<uint64_t>(Value), Comment);
1530b57cec5SDimitry Andric else
1540b57cec5SDimitry Andric emitEncodedSignedInteger(Value, Comment);
1550b57cec5SDimitry Andric } else if (isWriting()) {
1560b57cec5SDimitry Andric if (Value >= 0) {
1570b57cec5SDimitry Andric if (auto EC = writeEncodedUnsignedInteger(static_cast<uint64_t>(Value)))
1580b57cec5SDimitry Andric return EC;
1590b57cec5SDimitry Andric } else {
1600b57cec5SDimitry Andric if (auto EC = writeEncodedSignedInteger(Value))
1610b57cec5SDimitry Andric return EC;
1620b57cec5SDimitry Andric }
1630b57cec5SDimitry Andric } else {
1640b57cec5SDimitry Andric APSInt N;
1650b57cec5SDimitry Andric if (auto EC = consume(*Reader, N))
1660b57cec5SDimitry Andric return EC;
1670b57cec5SDimitry Andric Value = N.getExtValue();
1680b57cec5SDimitry Andric }
1690b57cec5SDimitry Andric
1700b57cec5SDimitry Andric return Error::success();
1710b57cec5SDimitry Andric }
1720b57cec5SDimitry Andric
mapEncodedInteger(uint64_t & Value,const Twine & Comment)1730b57cec5SDimitry Andric Error CodeViewRecordIO::mapEncodedInteger(uint64_t &Value,
1740b57cec5SDimitry Andric const Twine &Comment) {
1750b57cec5SDimitry Andric if (isStreaming())
1760b57cec5SDimitry Andric emitEncodedUnsignedInteger(Value, Comment);
1770b57cec5SDimitry Andric else if (isWriting()) {
1780b57cec5SDimitry Andric if (auto EC = writeEncodedUnsignedInteger(Value))
1790b57cec5SDimitry Andric return EC;
1800b57cec5SDimitry Andric } else {
1810b57cec5SDimitry Andric APSInt N;
1820b57cec5SDimitry Andric if (auto EC = consume(*Reader, N))
1830b57cec5SDimitry Andric return EC;
1840b57cec5SDimitry Andric Value = N.getZExtValue();
1850b57cec5SDimitry Andric }
1860b57cec5SDimitry Andric return Error::success();
1870b57cec5SDimitry Andric }
1880b57cec5SDimitry Andric
mapEncodedInteger(APSInt & Value,const Twine & Comment)1890b57cec5SDimitry Andric Error CodeViewRecordIO::mapEncodedInteger(APSInt &Value, const Twine &Comment) {
1900b57cec5SDimitry Andric if (isStreaming()) {
191*5f7ddb14SDimitry Andric // FIXME: We also need to handle big values here, but it's
192*5f7ddb14SDimitry Andric // not clear how we can excercise this code path yet.
1930b57cec5SDimitry Andric if (Value.isSigned())
1940b57cec5SDimitry Andric emitEncodedSignedInteger(Value.getSExtValue(), Comment);
1950b57cec5SDimitry Andric else
1960b57cec5SDimitry Andric emitEncodedUnsignedInteger(Value.getZExtValue(), Comment);
1970b57cec5SDimitry Andric } else if (isWriting()) {
1980b57cec5SDimitry Andric if (Value.isSigned())
199*5f7ddb14SDimitry Andric return writeEncodedSignedInteger(
200*5f7ddb14SDimitry Andric Value.isSingleWord() ? Value.getSExtValue() : INT64_MIN);
201*5f7ddb14SDimitry Andric return writeEncodedUnsignedInteger(Value.getLimitedValue());
2020b57cec5SDimitry Andric } else
2030b57cec5SDimitry Andric return consume(*Reader, Value);
2040b57cec5SDimitry Andric return Error::success();
2050b57cec5SDimitry Andric }
2060b57cec5SDimitry Andric
mapStringZ(StringRef & Value,const Twine & Comment)2070b57cec5SDimitry Andric Error CodeViewRecordIO::mapStringZ(StringRef &Value, const Twine &Comment) {
2080b57cec5SDimitry Andric if (isStreaming()) {
2090b57cec5SDimitry Andric auto NullTerminatedString = StringRef(Value.data(), Value.size() + 1);
2100b57cec5SDimitry Andric emitComment(Comment);
2115ffd83dbSDimitry Andric Streamer->emitBytes(NullTerminatedString);
2120b57cec5SDimitry Andric incrStreamedLen(NullTerminatedString.size());
2130b57cec5SDimitry Andric } else if (isWriting()) {
2140b57cec5SDimitry Andric // Truncate if we attempt to write too much.
2150b57cec5SDimitry Andric StringRef S = Value.take_front(maxFieldLength() - 1);
2160b57cec5SDimitry Andric if (auto EC = Writer->writeCString(S))
2170b57cec5SDimitry Andric return EC;
2180b57cec5SDimitry Andric } else {
2190b57cec5SDimitry Andric if (auto EC = Reader->readCString(Value))
2200b57cec5SDimitry Andric return EC;
2210b57cec5SDimitry Andric }
2220b57cec5SDimitry Andric return Error::success();
2230b57cec5SDimitry Andric }
2240b57cec5SDimitry Andric
mapGuid(GUID & Guid,const Twine & Comment)2250b57cec5SDimitry Andric Error CodeViewRecordIO::mapGuid(GUID &Guid, const Twine &Comment) {
2260b57cec5SDimitry Andric constexpr uint32_t GuidSize = 16;
2270b57cec5SDimitry Andric
2280b57cec5SDimitry Andric if (isStreaming()) {
2290b57cec5SDimitry Andric StringRef GuidSR =
2300b57cec5SDimitry Andric StringRef((reinterpret_cast<const char *>(&Guid)), GuidSize);
2310b57cec5SDimitry Andric emitComment(Comment);
2325ffd83dbSDimitry Andric Streamer->emitBytes(GuidSR);
2330b57cec5SDimitry Andric incrStreamedLen(GuidSize);
2340b57cec5SDimitry Andric return Error::success();
2350b57cec5SDimitry Andric }
2360b57cec5SDimitry Andric
2370b57cec5SDimitry Andric if (maxFieldLength() < GuidSize)
2380b57cec5SDimitry Andric return make_error<CodeViewError>(cv_error_code::insufficient_buffer);
2390b57cec5SDimitry Andric
2400b57cec5SDimitry Andric if (isWriting()) {
2410b57cec5SDimitry Andric if (auto EC = Writer->writeBytes(Guid.Guid))
2420b57cec5SDimitry Andric return EC;
2430b57cec5SDimitry Andric } else {
2440b57cec5SDimitry Andric ArrayRef<uint8_t> GuidBytes;
2450b57cec5SDimitry Andric if (auto EC = Reader->readBytes(GuidBytes, GuidSize))
2460b57cec5SDimitry Andric return EC;
2470b57cec5SDimitry Andric memcpy(Guid.Guid, GuidBytes.data(), GuidSize);
2480b57cec5SDimitry Andric }
2490b57cec5SDimitry Andric return Error::success();
2500b57cec5SDimitry Andric }
2510b57cec5SDimitry Andric
mapStringZVectorZ(std::vector<StringRef> & Value,const Twine & Comment)2520b57cec5SDimitry Andric Error CodeViewRecordIO::mapStringZVectorZ(std::vector<StringRef> &Value,
2530b57cec5SDimitry Andric const Twine &Comment) {
2540b57cec5SDimitry Andric
2550b57cec5SDimitry Andric if (!isReading()) {
2560b57cec5SDimitry Andric emitComment(Comment);
2570b57cec5SDimitry Andric for (auto V : Value) {
2580b57cec5SDimitry Andric if (auto EC = mapStringZ(V))
2590b57cec5SDimitry Andric return EC;
2600b57cec5SDimitry Andric }
2610b57cec5SDimitry Andric uint8_t FinalZero = 0;
2620b57cec5SDimitry Andric if (auto EC = mapInteger(FinalZero))
2630b57cec5SDimitry Andric return EC;
2640b57cec5SDimitry Andric } else {
2650b57cec5SDimitry Andric StringRef S;
2660b57cec5SDimitry Andric if (auto EC = mapStringZ(S))
2670b57cec5SDimitry Andric return EC;
2680b57cec5SDimitry Andric while (!S.empty()) {
2690b57cec5SDimitry Andric Value.push_back(S);
2700b57cec5SDimitry Andric if (auto EC = mapStringZ(S))
2710b57cec5SDimitry Andric return EC;
2720b57cec5SDimitry Andric };
2730b57cec5SDimitry Andric }
2740b57cec5SDimitry Andric return Error::success();
2750b57cec5SDimitry Andric }
2760b57cec5SDimitry Andric
emitEncodedSignedInteger(const int64_t & Value,const Twine & Comment)2770b57cec5SDimitry Andric void CodeViewRecordIO::emitEncodedSignedInteger(const int64_t &Value,
2780b57cec5SDimitry Andric const Twine &Comment) {
279*5f7ddb14SDimitry Andric // FIXME: There are no test cases covering this function.
280*5f7ddb14SDimitry Andric // This may be because we always consider enumerators to be unsigned.
281*5f7ddb14SDimitry Andric // See FIXME at CodeViewDebug.cpp : CodeViewDebug::lowerTypeEnum.
2820b57cec5SDimitry Andric if (Value >= std::numeric_limits<int8_t>::min()) {
2835ffd83dbSDimitry Andric Streamer->emitIntValue(LF_CHAR, 2);
2840b57cec5SDimitry Andric emitComment(Comment);
2855ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 1);
2860b57cec5SDimitry Andric incrStreamedLen(3);
2870b57cec5SDimitry Andric } else if (Value >= std::numeric_limits<int16_t>::min()) {
2885ffd83dbSDimitry Andric Streamer->emitIntValue(LF_SHORT, 2);
2890b57cec5SDimitry Andric emitComment(Comment);
2905ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 2);
2910b57cec5SDimitry Andric incrStreamedLen(4);
2920b57cec5SDimitry Andric } else if (Value >= std::numeric_limits<int32_t>::min()) {
2935ffd83dbSDimitry Andric Streamer->emitIntValue(LF_LONG, 2);
2940b57cec5SDimitry Andric emitComment(Comment);
2955ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 4);
2960b57cec5SDimitry Andric incrStreamedLen(6);
2970b57cec5SDimitry Andric } else {
2985ffd83dbSDimitry Andric Streamer->emitIntValue(LF_QUADWORD, 2);
2990b57cec5SDimitry Andric emitComment(Comment);
300*5f7ddb14SDimitry Andric Streamer->emitIntValue(Value, 4); // FIXME: Why not 8 (size of quadword)?
301*5f7ddb14SDimitry Andric incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
3020b57cec5SDimitry Andric }
3030b57cec5SDimitry Andric }
3040b57cec5SDimitry Andric
emitEncodedUnsignedInteger(const uint64_t & Value,const Twine & Comment)3050b57cec5SDimitry Andric void CodeViewRecordIO::emitEncodedUnsignedInteger(const uint64_t &Value,
3060b57cec5SDimitry Andric const Twine &Comment) {
3070b57cec5SDimitry Andric if (Value < LF_NUMERIC) {
3080b57cec5SDimitry Andric emitComment(Comment);
3095ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 2);
3100b57cec5SDimitry Andric incrStreamedLen(2);
3110b57cec5SDimitry Andric } else if (Value <= std::numeric_limits<uint16_t>::max()) {
3125ffd83dbSDimitry Andric Streamer->emitIntValue(LF_USHORT, 2);
3130b57cec5SDimitry Andric emitComment(Comment);
3145ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 2);
3150b57cec5SDimitry Andric incrStreamedLen(4);
3160b57cec5SDimitry Andric } else if (Value <= std::numeric_limits<uint32_t>::max()) {
3175ffd83dbSDimitry Andric Streamer->emitIntValue(LF_ULONG, 2);
3180b57cec5SDimitry Andric emitComment(Comment);
3195ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 4);
3200b57cec5SDimitry Andric incrStreamedLen(6);
3210b57cec5SDimitry Andric } else {
322*5f7ddb14SDimitry Andric // FIXME: There are no test cases covering this block.
3235ffd83dbSDimitry Andric Streamer->emitIntValue(LF_UQUADWORD, 2);
3240b57cec5SDimitry Andric emitComment(Comment);
3255ffd83dbSDimitry Andric Streamer->emitIntValue(Value, 8);
326*5f7ddb14SDimitry Andric incrStreamedLen(6); // FIXME: Why not 10 (8 + 2)?
3270b57cec5SDimitry Andric }
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric
writeEncodedSignedInteger(const int64_t & Value)3300b57cec5SDimitry Andric Error CodeViewRecordIO::writeEncodedSignedInteger(const int64_t &Value) {
3310b57cec5SDimitry Andric if (Value >= std::numeric_limits<int8_t>::min()) {
3320b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_CHAR))
3330b57cec5SDimitry Andric return EC;
3340b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<int8_t>(Value))
3350b57cec5SDimitry Andric return EC;
3360b57cec5SDimitry Andric } else if (Value >= std::numeric_limits<int16_t>::min()) {
3370b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_SHORT))
3380b57cec5SDimitry Andric return EC;
3390b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<int16_t>(Value))
3400b57cec5SDimitry Andric return EC;
3410b57cec5SDimitry Andric } else if (Value >= std::numeric_limits<int32_t>::min()) {
3420b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_LONG))
3430b57cec5SDimitry Andric return EC;
3440b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<int32_t>(Value))
3450b57cec5SDimitry Andric return EC;
3460b57cec5SDimitry Andric } else {
3470b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_QUADWORD))
3480b57cec5SDimitry Andric return EC;
3490b57cec5SDimitry Andric if (auto EC = Writer->writeInteger(Value))
3500b57cec5SDimitry Andric return EC;
3510b57cec5SDimitry Andric }
3520b57cec5SDimitry Andric return Error::success();
3530b57cec5SDimitry Andric }
3540b57cec5SDimitry Andric
writeEncodedUnsignedInteger(const uint64_t & Value)3550b57cec5SDimitry Andric Error CodeViewRecordIO::writeEncodedUnsignedInteger(const uint64_t &Value) {
3560b57cec5SDimitry Andric if (Value < LF_NUMERIC) {
3570b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(Value))
3580b57cec5SDimitry Andric return EC;
3590b57cec5SDimitry Andric } else if (Value <= std::numeric_limits<uint16_t>::max()) {
3600b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_USHORT))
3610b57cec5SDimitry Andric return EC;
3620b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(Value))
3630b57cec5SDimitry Andric return EC;
3640b57cec5SDimitry Andric } else if (Value <= std::numeric_limits<uint32_t>::max()) {
3650b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_ULONG))
3660b57cec5SDimitry Andric return EC;
3670b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint32_t>(Value))
3680b57cec5SDimitry Andric return EC;
3690b57cec5SDimitry Andric } else {
3700b57cec5SDimitry Andric if (auto EC = Writer->writeInteger<uint16_t>(LF_UQUADWORD))
3710b57cec5SDimitry Andric return EC;
3720b57cec5SDimitry Andric if (auto EC = Writer->writeInteger(Value))
3730b57cec5SDimitry Andric return EC;
3740b57cec5SDimitry Andric }
3750b57cec5SDimitry Andric
3760b57cec5SDimitry Andric return Error::success();
3770b57cec5SDimitry Andric }
378