Skip to content

Commit 824ce7a

Browse files
committed
Make GOFFWriter public
1 parent d335054 commit 824ce7a

File tree

2 files changed

+93
-84
lines changed

2 files changed

+93
-84
lines changed

llvm/include/llvm/MC/MCGOFFObjectWriter.h

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,97 @@
99
#ifndef LLVM_MC_MCGOFFOBJECTWRITER_H
1010
#define LLVM_MC_MCGOFFOBJECTWRITER_H
1111

12+
#include "llvm/BinaryFormat/GOFF.h"
1213
#include "llvm/MC/MCObjectWriter.h"
14+
#include "llvm/Support/Endian.h"
1315

1416
namespace llvm {
17+
class MCAssembler;
1518
class MCObjectWriter;
1619
class raw_pwrite_stream;
1720

21+
// The GOFFOstream is responsible to write the data into the fixed physical
22+
// records of the format. A user of this class announces the begin of a new
23+
// logical record. While writing the payload, the physical records are created
24+
// for the data. Possible fill bytes at the end of a physical record are written
25+
// automatically. In principle, the GOFFOstream is agnostic of the endianness of
26+
// the payload. However, it also supports writing data in big endian byte order.
27+
//
28+
// The physical records use the flag field to indicate if the there is a
29+
// successor and predecessor record. To be able to set these flags while
30+
// writing, the basic implementation idea is to always buffer the last seen
31+
// physical record.
32+
class GOFFOstream {
33+
/// The underlying raw_pwrite_stream.
34+
raw_pwrite_stream &OS;
35+
36+
/// The number of logical records emitted so far.
37+
uint32_t LogicalRecords = 0;
38+
39+
/// The number of physical records emitted so far.
40+
uint32_t PhysicalRecords = 0;
41+
42+
/// The size of the buffer. Same as the payload size of a physical record.
43+
static constexpr uint8_t BufferSize = GOFF::PayloadLength;
44+
45+
/// Current position in buffer.
46+
char *BufferPtr = Buffer;
47+
48+
/// Static allocated buffer for the stream.
49+
char Buffer[BufferSize];
50+
51+
/// The type of the current logical record, and the flags (aka continued and
52+
/// continuation indicators) for the previous (physical) record.
53+
uint8_t TypeAndFlags = 0;
54+
55+
public:
56+
GOFFOstream(raw_pwrite_stream &OS);
57+
~GOFFOstream();
58+
59+
raw_pwrite_stream &getOS();
60+
size_t getWrittenSize() const;
61+
uint32_t getNumLogicalRecords();
62+
63+
/// Write the specified bytes.
64+
void write(const char *Ptr, size_t Size);
65+
66+
/// Write zeroes, up to a maximum of 16 bytes.
67+
void write_zeros(unsigned NumZeros);
68+
69+
/// Support for endian-specific data.
70+
template <typename value_type> void writebe(value_type Value) {
71+
Value =
72+
support::endian::byte_swap<value_type>(Value, llvm::endianness::big);
73+
write((const char *)&Value, sizeof(value_type));
74+
}
75+
76+
/// Begin a new logical record. Implies finalizing the previous record.
77+
void newRecord(GOFF::RecordType Type);
78+
79+
/// Ends a logical record.
80+
void finalizeRecord();
81+
82+
private:
83+
/// Updates the continued/continuation flags, and writes the record prefix of
84+
/// a physical record.
85+
void updateFlagsAndWritePrefix(bool IsContinued);
86+
87+
/// Returns the remaining size in the buffer.
88+
size_t getRemainingSize();
89+
};
90+
91+
class GOFFWriter {
92+
GOFFOstream OS;
93+
[[maybe_unused]] MCAssembler &Asm;
94+
95+
void writeHeader();
96+
void writeEnd();
97+
98+
public:
99+
GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm);
100+
uint64_t writeObject();
101+
};
102+
18103
class MCGOFFObjectTargetWriter : public MCObjectTargetWriter {
19104
protected:
20105
MCGOFFObjectTargetWriter() = default;

llvm/lib/MC/GOFFObjectWriter.cpp

Lines changed: 8 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -61,82 +61,20 @@ constexpr uint8_t RecContinued = Flags(7, 1, 1);
6161

6262
// Flag: This record is a continuation.
6363
constexpr uint8_t RecContinuation = Flags(6, 1, 1);
64-
65-
// The GOFFOstream is responsible to write the data into the fixed physical
66-
// records of the format. A user of this class announces the begin of a new
67-
// logical record. While writing the payload, the physical records are created
68-
// for the data. Possible fill bytes at the end of a physical record are written
69-
// automatically. In principle, the GOFFOstream is agnostic of the endianness of
70-
// the payload. However, it also supports writing data in big endian byte order.
71-
//
72-
// The physical records use the flag field to indicate if the there is a
73-
// successor and predecessor record. To be able to set these flags while
74-
// writing, the basic implementation idea is to always buffer the last seen
75-
// physical record.
76-
class GOFFOstream {
77-
/// The underlying raw_pwrite_stream.
78-
raw_pwrite_stream &OS;
79-
80-
/// The number of logical records emitted so far.
81-
uint32_t LogicalRecords = 0;
82-
83-
/// The number of physical records emitted so far.
84-
uint32_t PhysicalRecords = 0;
85-
86-
/// The size of the buffer. Same as the payload size of a physical record.
87-
static constexpr uint8_t BufferSize = GOFF::PayloadLength;
88-
89-
/// Current position in buffer.
90-
char *BufferPtr = Buffer;
91-
92-
/// Static allocated buffer for the stream.
93-
char Buffer[BufferSize];
94-
95-
/// The type of the current logical record, and the flags (aka continued and
96-
/// continuation indicators) for the previous (physical) record.
97-
uint8_t TypeAndFlags = 0;
98-
99-
public:
100-
GOFFOstream(raw_pwrite_stream &OS);
101-
~GOFFOstream();
102-
103-
raw_pwrite_stream &getOS() { return OS; }
104-
size_t getWrittenSize() const { return PhysicalRecords * GOFF::RecordLength; }
105-
uint32_t getNumLogicalRecords() { return LogicalRecords; }
106-
107-
/// Write the specified bytes.
108-
void write(const char *Ptr, size_t Size);
109-
110-
/// Write zeroes, up to a maximum of 16 bytes.
111-
void write_zeros(unsigned NumZeros);
112-
113-
/// Support for endian-specific data.
114-
template <typename value_type> void writebe(value_type Value) {
115-
Value =
116-
support::endian::byte_swap<value_type>(Value, llvm::endianness::big);
117-
write((const char *)&Value, sizeof(value_type));
118-
}
119-
120-
/// Begin a new logical record. Implies finalizing the previous record.
121-
void newRecord(GOFF::RecordType Type);
122-
123-
/// Ends a logical record.
124-
void finalizeRecord();
125-
126-
private:
127-
/// Updates the continued/continuation flags, and writes the record prefix of
128-
/// a physical record.
129-
void updateFlagsAndWritePrefix(bool IsContinued);
130-
131-
/// Returns the remaining size in the buffer.
132-
size_t getRemainingSize();
133-
};
13464
} // namespace
13565

13666
GOFFOstream::GOFFOstream(raw_pwrite_stream &OS) : OS(OS) {}
13767

13868
GOFFOstream::~GOFFOstream() { finalizeRecord(); }
13969

70+
raw_pwrite_stream &GOFFOstream::getOS() { return OS; }
71+
72+
size_t GOFFOstream::getWrittenSize() const {
73+
return PhysicalRecords * GOFF::RecordLength;
74+
}
75+
76+
uint32_t GOFFOstream::getNumLogicalRecords() { return LogicalRecords; }
77+
14078
void GOFFOstream::updateFlagsAndWritePrefix(bool IsContinued) {
14179
// Update the flags based on the previous state and the flag IsContinued.
14280
if (TypeAndFlags & RecContinued)
@@ -222,20 +160,6 @@ void GOFFOstream::finalizeRecord() {
222160
BufferPtr = Buffer;
223161
}
224162

225-
namespace {
226-
class GOFFWriter {
227-
GOFFOstream OS;
228-
[[maybe_unused]] MCAssembler &Asm;
229-
230-
void writeHeader();
231-
void writeEnd();
232-
233-
public:
234-
GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm);
235-
uint64_t writeObject();
236-
};
237-
} // namespace
238-
239163
GOFFWriter::GOFFWriter(raw_pwrite_stream &OS, MCAssembler &Asm)
240164
: OS(OS), Asm(Asm) {}
241165

0 commit comments

Comments
 (0)