LLVM 23.0.0git
FileWriter.cpp
Go to the documentation of this file.
1//===- FileWriter.cpp -------------------------------------------*- C++ -*-===//
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-exception
6//
7//===----------------------------------------------------------------------===//
8
10#include "llvm/Support/LEB128.h"
12#include <cassert>
13
14using namespace llvm;
15using namespace gsym;
16
17FileWriter::~FileWriter() { OS.flush(); }
18
19void FileWriter::writeSLEB(int64_t S) {
20 uint8_t Bytes[32];
21 auto Length = encodeSLEB128(S, Bytes);
22 assert(Length < sizeof(Bytes));
23 OS.write(reinterpret_cast<const char *>(Bytes), Length);
24}
25
27 uint8_t Bytes[32];
28 auto Length = encodeULEB128(U, Bytes);
29 assert(Length < sizeof(Bytes));
30 OS.write(reinterpret_cast<const char *>(Bytes), Length);
31}
32
34 OS.write(reinterpret_cast<const char *>(&U), sizeof(U));
35}
36
38 const uint16_t Swapped = support::endian::byte_swap(U, ByteOrder);
39 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
40}
41
43 const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
44 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
45}
46
48 const uint64_t Swapped = support::endian::byte_swap(U, ByteOrder);
49 OS.write(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped));
50}
51
53 assert(ByteSize <= 8 && "invalid byte size");
54 // Make sure the value fits in the number of bytes specified.
55 assert((ByteSize == 8 || (Value & (uint64_t)-1 << (8 * ByteSize)) == 0) &&
56 "potential data loss: higher bits are non-zero");
57 // Swap and shift bytes if endianness doesn't match.
58 if (ByteOrder != llvm::endianness::native) {
59 // Say ByteSize is 3.
60 // high low
61 // Input bytes: 00 00 00 00 00 AA BB CC
62 // Swapped bytes: CC BB AA 00 00 00 00 00
63 // Shifted bytes: 00 00 00 00 00 CC BB AA
64 Value = sys::getSwappedBytes(Value) >> (8 * (8 - ByteSize));
65 }
66 OS.write(reinterpret_cast<const char *>(&Value), ByteSize);
67}
68
70 const uint32_t Swapped = support::endian::byte_swap(U, ByteOrder);
71 OS.pwrite(reinterpret_cast<const char *>(&Swapped), sizeof(Swapped),
72 Offset);
73}
74
76 OS.write(reinterpret_cast<const char *>(Data.data()), Data.size());
77}
78
80 OS << Str << '\0';
81}
82
84 return OS.tell();
85}
86
88 off_t Offset = OS.tell();
89 off_t AlignedOffset = (Offset + Align - 1) / Align * Align;
90 if (AlignedOffset == Offset)
91 return;
92 off_t PadCount = AlignedOffset - Offset;
93 OS.write_zeros(PadCount);
94}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI uint64_t tell()
Return the current offset within the file.
LLVM_ABI void writeULEB(uint64_t Value)
Write the value into the stream encoded using unsigned LEB128 at the current file position.
LLVM_ABI void writeU16(uint16_t Value)
Write a single uint16_t value into the stream at the current file position.
LLVM_ABI void fixup32(uint32_t Value, uint64_t Offset)
Fixup a uint32_t value at the specified offset in the stream.
LLVM_ABI void writeSLEB(int64_t Value)
Write the value into the stream encoded using signed LEB128 at the current file position.
LLVM_ABI void writeU8(uint8_t Value)
Write a single uint8_t value into the stream at the current file position.
LLVM_ABI void alignTo(size_t Align)
Pad with zeroes at the current file position until the current file position matches the specified al...
LLVM_ABI void writeU32(uint32_t Value)
Write a single uint32_t value into the stream at the current file position.
LLVM_ABI void writeData(llvm::ArrayRef< uint8_t > Data)
Write an array of uint8_t values into the stream at the current file position.
LLVM_ABI void writeNullTerminated(llvm::StringRef Str)
Write a NULL terminated C string into the stream at the current file position.
LLVM_ABI void writeUnsigned(uint64_t Value, size_t ByteSize)
Write a single unsigned value into the stream at the current file position.
LLVM_ABI void writeU64(uint64_t Value)
Write a single uint64_t value into the stream at the current file position.
value_type byte_swap(value_type value, endianness endian)
Definition Endian.h:44
unsigned char getSwappedBytes(unsigned char C)
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:532
@ Length
Definition DWP.cpp:532
FunctionAddr VTableAddr uintptr_t uintptr_t Data
Definition InstrProf.h:221
unsigned encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a SLEB128 value to an output stream.
Definition LEB128.h:24
unsigned encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned PadTo=0)
Utility function to encode a ULEB128 value to an output stream.
Definition LEB128.h:79
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39